package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.UserDTO;
import com.sky.dto.UserPageQueryDTO;
import com.sky.entity.User;
import com.sky.exception.BusinessException;
import com.sky.mapper.UserMapper;
import com.sky.result.PageResult;
import com.sky.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 新增用户
     * @param userDTO
     */
    @Transactional
    public void save(UserDTO userDTO) {
        log.info("开始新增用户，参数：{}", userDTO);

        // 验证微信号是否已存在
        if (userDTO.getOpenid() != null) {
            User existingUser = userMapper.getByOpenid(userDTO.getOpenid());
            if (existingUser != null) {
                log.warn("新增用户失败：微信号{}已存在", userDTO.getOpenid());
                throw new BusinessException("微信号已存在");
            }
        }

        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        user.setCreateTime(LocalDateTime.now());

        userMapper.insert(user);
        log.info("新增用户成功");
    }

    /**
     * 删除用户
     * @param id
     */
    @Transactional
    public void delete(Long id) {
        log.info("开始删除用户，id：{}", id);

        // 验证用户是否存在
        User user = userMapper.getById(id);
        if (user == null) {
            log.warn("删除用户失败：用户{}不存在", id);
            throw new BusinessException("用户不存在");
        }

        userMapper.delete(id);
        log.info("删除用户成功");
    }

    /**
     * 编辑用户信息
     * @param userDTO
     */
    @Transactional
    public void update(UserDTO userDTO) {
        log.info("开始更新用户信息，参数：{}", userDTO);

        // 验证用户是否存在
        if (userDTO.getId() == null) {
            log.warn("更新用户失败：用户id为空");
            throw new BusinessException("用户id不能为空");
        }

        User existingUser = userMapper.getById(userDTO.getId());
        if (existingUser == null) {
            log.warn("更新会员失败：用户不存在");
            throw new BusinessException("用户不存在");
        }

        User user = new User();
        BeanUtils.copyProperties(userDTO, user);
        user.setUpdateTime(LocalDateTime.now());

        userMapper.update(user);
        log.info("更新用户成功");
    }

    /**
     * 用户分页查询
     * @param userPageQueryDTO
     * @return
     */
    public PageResult pageQuery(UserPageQueryDTO userPageQueryDTO) {
        log.info("开始用户分页查询，参数：{}", userPageQueryDTO);
        PageHelper.startPage(userPageQueryDTO.getPage(), userPageQueryDTO.getPageSize());
        Page<User> page = userMapper.pageQuery(userPageQueryDTO);
        long total = page.getTotal();
        List<User> records = page.getResult();
        log.info("用户分页查询完成，总记录数：{}, 当前页记录数：{}", total, records.size());
        return new PageResult(total, records);
    }

    /**
     * 根据id查询用户信息
     * @param id
     * @return
     */
    public User getById(Long id) {
        log.info("根据id查询，id：{}", id);
        User user = userMapper.getById(id);
        log.info("查询结果：{}", user);
        return user;
    }
} 